package com.atguigu.demo.customer.controller;

import com.atguigu.demo.customer.bean.Customer;
import com.atguigu.demo.customer.service.CustomerService;
import com.atguigu.demo.customer.service.impl.CustomerServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CustomerController {

    @Autowired
    CustomerService customerService;
    @RequestMapping(value = "/sayhello",method = RequestMethod.GET)
    String sayHello(){
         return  " hello world 0717";
    }


   // @RequestMapping(value = "/customer",method = RequestMethod.GET)
    @GetMapping("/customer")
    String getCustomer( @RequestParam("name") String name){
        return "{\"id\":\"123\",\"name\":\""+name+"\",\"age\":33}";
    }

    @GetMapping("/customer/{id}")
    Customer getCustomerById( @PathVariable("id") Long id ){
       // Customer customer =customerService.getCustomer(id);
        Customer customer =  customerService.getById(id);

        return customer;
    }

    @GetMapping("/customers")
    List<Customer>  getCustomerList( Customer customer){
       // List<Customer> customerList= customerService.getCustomerList(customer);

//        if(customer.getName()!=null) {
//            queryWrapper.like("name",customer.getName());
//        }
        QueryWrapper queryWrapper=  new QueryWrapper<>();
        queryWrapper.like(customer.getName()!=null,"name",customer.getName());
        queryWrapper.ge(customer.getAge()!=null,"age",customer.getAge());

        List<Customer> customerList= customerService.list(queryWrapper);


        return  customerList;
    }

    @PostMapping("/customer")
    String saveCustomer( @RequestBody  Customer customer){
        System.out.println(customer+"1111112222222222222222221");
        customerService.saveOrUpdate(customer);
        //customerService.saveCustomer(customer);
        return "success";
    }


}
